package com.cos.hospital.tool;

import android.content.SharedPreferences;

import com.cos.hospital.code.Code;
import com.cos.hospital.model.bean.user.TokenBean;
import com.jude.utils.JUtils;


/**
 * @author yby
 * @description SharedPreferences工具类
 */
public class SPUtil {
    private static SharedPreferences sp_userInfo = JUtils.getSharedPreference(Code.SP.USER_INFO);
    private static SharedPreferences sp_startApp = JUtils.getSharedPreference(Code.SP.START_APP);

    public static void saveToken(TokenBean tokenBean) {
        if (tokenBean != null) {
            String access_token = tokenBean.getAccess_token();
            sp_userInfo.edit()
                .putString(Code.SP.TOKEN, access_token)
                .putBoolean(Code.SP.LOGIN, true)
                .apply();
        }
    }

    public static void saveDBVerison(int version) {
        sp_userInfo.edit()
            .putInt(Code.SP.DB_VERSION, version)
            .apply();
    }

    public static int getDBVersion() {
        return sp_userInfo.getInt(Code.SP.DB_VERSION, -1);
    }

    public static void saveToken(String token) {
        sp_userInfo.edit().putString(Code.SP.TOKEN, token).apply();
    }

    public static String getToken() {
        return sp_userInfo.getString(Code.SP.TOKEN, "");
    }

    public static void saveId(int id) {
        sp_userInfo.edit().putInt(Code.SP.ID, id).apply();
    }

    public static int getUserId() {
        return sp_userInfo.getInt(Code.SP.ID, 0);
    }


    public static boolean isLogin() {
        return sp_userInfo.getBoolean(Code.SP.LOGIN, false);
    }

    public static void clearUserInfo() {
        sp_userInfo.edit().clear().apply();
    }

    public static void saveFirstStartApp(boolean flag) {
        sp_startApp.edit().putBoolean(Code.SP.FIRST_START_APP, flag).apply();
    }

    public static boolean isFirstStartApp() {
        return sp_startApp.getBoolean(Code.SP.FIRST_START_APP, true);
    }

    public static void saveNewestVersion(boolean yby) {
        sp_startApp.edit().putBoolean(JUtils.getAppVersionName(), yby).apply();
    }

    public static boolean isNewestVersion() {
        return sp_startApp.getBoolean(JUtils.getAppVersionName(), false);
    }

}